package cn.net.mugui.net.pc.panel.view;

import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.lang3.StringUtils;

import com.mugui.Dui.DTextField;
import com.mugui.Dui.DVerticalFlowLayout;
import com.mugui.base.base.Autowired;

import cn.net.mugui.net.dao.Dao;
import cn.net.mugui.net.p2p.ServerAttr;
import cn.net.mugui.net.p2p.ServerAttr.ServerAttrListener;
import lombok.Getter;

import java.awt.Color;
import java.awt.Font;
import com.mugui.Dui.DButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserView extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DTextField user_address;
	private JLabel server_status;
	private JLabel server_type;
	private JLabel port;
	private JLabel host;

	public UserView() {
		this.setLayout(new DVerticalFlowLayout());
		JPanel panel_12 = new JPanel();
		panel_12.setBackground(Color.GRAY);
		this.add(panel_12);

		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_1.setBackground(null);

		JLabel label_3 = new JLabel("用户名：");
		panel_1.add(label_3);
		label_3.setFont(new Font("宋体", Font.PLAIN, 14));

		user_name = new JLabel("New label");
		panel_1.add(user_name);
		user_name.setFont(new Font("宋体", Font.BOLD, 12));

		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout_7 = (FlowLayout) panel_2.getLayout();
		flowLayout_7.setVgap(0);
		panel_2.setBackground(null);

		editBtn = new DButton((String) null, (Color) null);
		editBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (listener != null) {
					listener.actionPerformed(new ActionEvent(UserView.this, 1, "edit"));
				}
			}
		});
		
		update_btn = new DButton((String) null, (Color) null);
		update_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (listener != null) {
					listener.actionPerformed(new ActionEvent(UserView.this, 3, "update"));
				}
			}
		});
		update_btn.setText("刷新");
		update_btn.setFont(new Font("Dialog", Font.BOLD, 13));
		panel_2.add(update_btn);
		editBtn.setFont(new Font("Dialog", Font.BOLD, 13));
		editBtn.setText("编辑");
		panel_2.add(editBtn);

		removeBtn = new DButton((String) null, (Color) null);
		removeBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (listener != null) {
					listener.actionPerformed(new ActionEvent(UserView.this, 2, "remove"));
				}
			}
		});
		removeBtn.setFont(new Font("Dialog", Font.BOLD, 13));
		removeBtn.setText("删除");
		panel_2.add(removeBtn);
		GroupLayout gl_panel_12 = new GroupLayout(panel_12);
		gl_panel_12.setHorizontalGroup(gl_panel_12.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_12.createSequentialGroup()
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)));
		gl_panel_12.setVerticalGroup(gl_panel_12.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_12.createSequentialGroup()
						.addGroup(gl_panel_12.createParallelGroup(Alignment.TRAILING)
								.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
								.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))));
		panel_12.setLayout(gl_panel_12);

		JPanel panel_13 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_13.getLayout();
		flowLayout_2.setHgap(3);
		flowLayout_2.setVgap(0);
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		this.add(panel_13);

		JPanel panel_15 = new JPanel();
		panel_13.add(panel_15);

		JLabel label_4 = new JLabel("地址:");
		panel_13.add(label_4);

		user_address = new DTextField(64);
		user_address.setFont(new Font("微软雅黑", Font.PLAIN, 11));
		user_address.setColumns(24);
		user_address.setText("lkajsdlfkjlasjdlfj;laksjdl;kfj");
		user_address.setBackground(null);
		user_address.setEditable(false);
		user_address.setBorder(null);
		panel_13.add(user_address);

		JPanel panel_14 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_14.getLayout();
		flowLayout_3.setHgap(3);
		flowLayout_3.setVgap(0);
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		this.add(panel_14);

		JPanel panel_16 = new JPanel();
		panel_14.add(panel_16);

		JLabel label_5 = new JLabel("状态:");
		panel_14.add(label_5);

		server_status = new JLabel("连接中");
		panel_14.add(server_status);

		JPanel panel_17 = new JPanel();
		FlowLayout flowLayout_6 = (FlowLayout) panel_17.getLayout();
		flowLayout_6.setHgap(6);
		panel_14.add(panel_17);

		JPanel panel_21 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_21.getLayout();
		flowLayout_5.setHgap(7);
		panel_14.add(panel_21);

		JLabel label_7 = new JLabel("类型:");
		panel_14.add(label_7);

		server_type = new JLabel("公网");
		panel_14.add(server_type);

		JPanel panel_18 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_18.getLayout();
		flowLayout_4.setHgap(3);
		flowLayout_4.setVgap(0);
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		this.add(panel_18);

		JPanel panel_19 = new JPanel();
		panel_18.add(panel_19);

		JLabel lblHost = new JLabel("host:");
		panel_18.add(lblHost);

		host = new JLabel("127.0.0.1");
		panel_18.add(host);

		JPanel panel_20 = new JPanel();
		panel_18.add(panel_20);

		JLabel lblPort = new JLabel("port:");
		panel_18.add(lblPort);

		port = new JLabel("8090");
		panel_18.add(port);

		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setVgap(1);
		flowLayout.setHgap(1);
		add(panel);
	}

	private ActionListener listener = null;

	public void addActionListener(ActionListener listener) {
		this.listener = listener;
	}

	@Getter
	private ServerAttr serverAttr = null;

	/**
	 * 绑定用户数据
	 * 
	 * @author 木鬼
	 * @param attr
	 * @return
	 */
	public UserView bind(ServerAttr attr) {
		this.serverAttr = attr;
		serverAttr.listener(serverAttrListener);
		updateView();
		return this;
	}

	private ServerAttrListener serverAttrListener = new ServerAttrListener() {
		@Override
		public void listener(ServerAttr attr) {
			bind(attr);
		}

	};

	@Override
	public void removeAll() {
		listener = null;
		super.removeAll();
	}

	private Dao dao = Dao.getInstance();
	private JLabel user_name;
	private DButton editBtn;
	private DButton removeBtn;
	private DButton update_btn;

	/**
	 * 重新加载数据
	 * 
	 * @author 木鬼
	 */
	public void validateView() {
		serverAttr = dao.get(serverAttr);
		updateView();
	}

	/**
	 * 更新UI数据
	 * 
	 * @author 木鬼
	 */
	public void updateView() {
		user_address.setText(serverAttr.getAddress());

		if (serverAttr.getAddress().equals(ServerAttr.self.getAddress())) {
			user_name.setText("自身："
					+ (StringUtils.isBlank(serverAttr.getName()) ? serverAttr.getAddress() : serverAttr.getName()));
			editBtn.setEnabled(false);
			removeBtn.setEnabled(false);
			update_btn.setEnabled(false);
		} else
			user_name.setText(StringUtils.isBlank(serverAttr.getName()) ? "" : serverAttr.getName());
		if (serverAttr.getPort() == null) {
			serverAttr.setPort(0);
		}
		port.setText(serverAttr.getPort().toString());
		if (StringUtils.isBlank(serverAttr.getHost())) {
			serverAttr.setHost("127.0.0.0");
		}
		host.setText(serverAttr.getHost().toString());
		String status = "未知";
		switch (serverAttr.getStatus()) {
		case ServerAttr.status_0:
			status = "已断开";
			break;
		case ServerAttr.status_1:
			status = "待连接";
			break;
		case ServerAttr.status_2:
			status = "连接中";
			break;
		case ServerAttr.status_3:
			status = serverAttr.getPing() + " ms";
			break;
		default:
			break;
		}
		server_status.setText(status);

		String type = "未知";
		switch (serverAttr.getType()) {
		case ServerAttr.type_0:
			type = "未知类型";
			break;
		case ServerAttr.type_1:
			type = "公网";
			break;
		case ServerAttr.type_2:
			type = "固定host,固定port";
			break;
		case ServerAttr.type_3:
			type = "固定host,动态port";
			break;
		default:
			break;
		}
		server_type.setText(type);
	}

	/**
	 * 创建一个新的UI
	 * 
	 * @author 木鬼
	 * @param attr
	 * @return
	 */
	public static UserView newView(ServerAttr attr) {
		return new UserView().bind(attr);
	}
}
